#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"Contact.h"

void SLinit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLdes(SL* ps) {

	if(ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLcheck(SL* ps) {
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLdatatype* tmp=(SLdatatype*)realloc(ps->arr, newcapacity * sizeof(SLdatatype));
		if (tmp == NULL) {
			perror("realloc");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

void SLpushback(SL* ps,SLdatatype x) {
	SLcheck(ps);
	assert(ps);

	
	ps->arr[ps->size++] = x;
}

void SLPrint(SL s) {
	for (int i = 0; i < s.size; i++)
		printf("%d\n", s.arr[i]);
}

void SLpushfront(SL* ps,SLdatatype x) {
	SLcheck(ps);
	assert(ps);

	for (int i = ps->size; i > 0; i--) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

void SLpopback(SL* ps) {
	assert(ps);

	ps->size--;
}

void SLpopfront(SL* ps) {
	assert(ps);

	for (int i = 0; i < ps->size-1; i++) {
		ps->arr[i] = ps->arr[i+1];
	}
	ps->size--;
}

void SLInsert(SL* ps, int pos,SLdatatype x) {
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	SLcheck(ps);
	for (int i = ps->size;i>pos; i--) {
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	for (int i = pos;i<ps->size-1; i++) {
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//int SLFind(SL* ps, SLdatatype x) {
//	assert(ps);
//
//	for (int i = 0; i < ps->size; i++) {
//		if (x == ps->arr[i])
//			return i;
//	}
//	return -1;
//}
